package com.example.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import com.nanhang.common.util.PageResult;
import com.nanhang.common.util.R;
import com.example.entity.Teacher;
import com.example.service.ITeacherService;
import com.example.vo.TeacherVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


@Controller
@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private ITeacherService staffService;

    // 员工列表
    @RequestMapping("/list")
    public R getStaff() {
        return R.ok().data("list",staffService.list());
    }

    // 分页查询
    @GetMapping("/findByPage/{page}/{pageSize}")
    public R findByPage(@PathVariable("page") Integer page,
                        @PathVariable("pageSize") Integer pageSize) {
        PageResult<Teacher> pr = staffService.findByPage(page,pageSize);
        return R.ok().data("pr",pr);
    }
    // 条件带分页查询
    @PostMapping("/search/{page}/{pageSize}")
    public R search(@PathVariable("page") Integer page,
                    @PathVariable("pageSize") Integer pageSize,
                    @RequestBody(required = false) TeacherVO vo){
        PageResult<Teacher> pr = staffService.search(page,pageSize,vo);
        return R.ok().data("pr",pr);
    }

    // 添加员工
    @PostMapping("/add")
    public R add(@RequestBody Teacher staff){
        boolean flag = staffService.add(staff);
        return flag ? R.ok().message("添加员工成功!") : R.error().message("添加员工失败!");
    }

    //修改员工
    @PutMapping("/update")
    public R update(@RequestBody Teacher staff){
        boolean flag = staffService.updateById(staff);
        return flag ? R.ok().message("修改员工成功!") : R.error().message("修改员工失败!");
    }


    // 删除员工
    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable("ids") String ids){  // 1,2,3
        String[] split = ids.split(",");
        List<String> sids = Arrays.asList(split);
        return staffService.removeByIds(sids) ? R.ok().message("删除员工成功!") : R.error().message("删除员工失败!");
    }
}
